<?php 
/* search class that allows to search the site and the web 
 * as well as many other things the bing api allows to query */
class search{
	private $db;
	private $b;
	
	// instanciates this class and
	// the private local database
	public function search($domain){
		$b = new blog($domain);
		$this->db = $b->db;
	}
	
	/* sorts search results retrieved 
	 * with the 'searchSite' method */
	private function sortResult($unsorted){
		$result = array();
		// create sortable array
		$sortable = array();
		for($r=0;$r<count($unsorted);$r++){
			$sortable[$r] = $unsorted[$r]["index"]."-".$unsorted[$r]["url"];
		}
	
		// sort the sortable array
		natsort($sortable);

		// replace the sorted with the real object vals
		$s = 0;
		foreach($sortable as $sorted){
			for($i=0;$i<count($unsorted);$i++){
				if($sorted==$unsorted[$i]["index"]."-".$unsorted[$i]["url"]){
					$result[$s] = $unsorted[$i];
				}
			}
			$s++;
		}
		
		// finally return the sorted result reversed
		// as the sort will have put the result with
		// the lowest index value on top, but we want
		// to have the highest index values on top
		return array_reverse($result);
	}
	
	/* searches the site for articles matching the query */
	public function searchSite($query){
		$result = $this->db->searchArticle($query);
		
		/* scan for spaces and query each individual word adding 
		 * its index value to the total index value of the page */
		if(strpos($query, " ")!=false){
			$sq = explode(" ",$query);
			for($w=0;$w<count($sq);$w++){
				$w_result = $this->db->searchArticle($sq[$w]);
				for($r=0;$r<count($w_result);$r++){
					$already_found = false;
					
					/* check if the result is already in the resultset */
					for($t=0;$t<count($result);$t++){
						if($result[$t]["url"]==$w_result[$r]["url"]){
							$already_found = true;
							// add the index value of this result to the existing one
							$result[$t]["index"] = $result[$t]["index"] + $w_result[$r]["index"];
						}
					}
					
					/* if its not yet in the resultset, add it */
					if($already_found==false){
						$result[count($result)] = $w_result[$r];
					}
				}
			}
		}
		
		/* calculate the total index value and 
		 * define the percentage for each result item */
		$total_value = 0;
		for($x=0;$x<count($result);$x++){
			$total_value += $result[$x]["index"];
		}
		/* add the percentage value to each item */
		for($p=0;$p<count($result);$p++){
			$result[$p]["index_percentage"] = 0;
			if($total_value>0){
				$result[$p]["index_percentage"] 
					= ($result[$p]["index"] / $total_value);
			}
		}
		
		return $this->sortResult($result);
	}
	
	/* searches twitter for a specific query */
	public function searchTwitter($query,$count){
		// only use the first word when multiple words exist
		// in the query string as otherwise we might not have
		// enough results from twitter
		if(strpos($query," ")!=false){
			$qw = explode(" ",$query);
			$query = $qw[0];
		}
		
		// create the twitter api search url with the hash-tag
		$url = "http://search.twitter.com/search.json?q=%23".$query;
		$json_result = json_decode(file_get_contents($url));
		$result = $json_result->results;
		
		if(count($result)>$count){
			$t_result = array();
			for($i=0;$i<$count;$i++){
				$t_result[$i] = $result[$i];
			}
			$result = $t_result;
		}
		
		return $result;
	}
	
	/* executes a bing search and returns the simple xml result object */
	public function searchWeb($query,$count){		
		// create the api search url for bing using siegburg lat/lng
		$search_url = "http://api.bing.net/xml.aspx?AppId=".CONFIG_BING_API_KEY
					."&Version=2.2&Market=de-DE&Query=".urldecode($query)
					."&UILanguage=de&Latitude=50.8022099&Longitude=7.1943825"
					."&Web.Count=".$count."&Sources=Web+Spell+RelatedSearch+News+Image";
		
		// change the namespace in the raw results
		$searchResults = file_get_contents($search_url);
		$searchResults = str_replace("web:", "web_", $searchResults);
					
		// get the search result and return them
		return simplexml_load_string($searchResults);
	}
}
?>